package com.fuwenhao;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author fwh
 * @email fuwenhao594@163.com
 * @date 2019/7/25 下午4:58
 */
@Service
public class HelloService {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * HystrixCommand  指定调用失败后返回的方法
     *
     * @param
     * @return java.lang.String
     * @author fwh [2019/7/25 && 下午5:42]
     */
    @HystrixCommand(fallbackMethod = "error")
    public String hello() {
        final ResponseEntity<String> forEntity = restTemplate.getForEntity("http://fwh-ribbon-server/hello", String.class);
        final String body = forEntity.getBody();
        return body;
    }

    /**
     * 失败后返回的方法测试
     *
     * @param
     * @return java.lang.String
     * @author fwh [2019/7/25 && 下午5:42]
     */
    public String error() {
        return "hystrix error";
    }
}
